﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Presupuesto;
using System.Data;
using System.Collections;
using System.Web.Security;
namespace Interfaces.Presupuesto
{
    public partial class FormularioPresupuesto : System.Web.UI.Page
    {

    //ICollection CreateDataSource() 
    //{

    //    // Create sample data for the DataList control.
    //    DataTable dt = new DataTable();
    //    DataRow dr;

    //    // Define the columns of the table.
    //    dt.Columns.Add(new DataColumn("ProductName", typeof(String)));
    //    dt.Columns.Add(new DataColumn("ProductName2", typeof(String)));
    //    // Populate the table with sample values.
    //    for (int i = 0; i < 9; i++) 
    //    {
    //    dr = dt.NewRow();

    //    dr[0] = i.ToString();
    //    dr[1] = "Description for item " + i.ToString();
            
    //    dt.Rows.Add(dr);
    //    }

    //    DataView dv = new DataView(dt);
    //    return dv;

    //}


        private int gest
        {
            set { ViewState["gest"] = value; }
            get { return (int)ViewState["gest"]; }
        }
        private long codigo
        {
            set { ViewState["cod"] = value; }
            get { return (long)ViewState["cod"]; }
        }
        private string codigoEvento
        {
            set { ViewState["cod"] = value; }
            get { return ViewState["cod"].ToString(); }
        }
        private char tipo
        {
            set { ViewState["tipo"] = value; }
            get { return (char)ViewState["tipo"]; }
        }
        public void Bind()
        {
            DataList1.DataSource = Processes.Presupuesto.GestorPresupuesto.getIngresos(gest,tipo);
            DataList1.DataBind();
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
         
            if (!IsPostBack)
            {
                if (Request.QueryString["Gestion"] == null)
                {
                    LabelError.Text = "No especifico gestion";
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    try
                    {
                        gest = Convert.ToInt32(Request.QueryString["Gestion"]);

                        if (!Processes.Presupuesto.GestorPresupuesto.getEstado(gest.ToString()))
                        {
                            LabelError.Text = "Esta gestion se encuentra cerrada";
                            MultiView1.ActiveViewIndex = 1;
                        }
                        else
                        {
                            if (Request.QueryString["Tipo"] == null)
                            {
                                LabelError.Text = "No ingreso ningun tipo";
                                MultiView1.ActiveViewIndex = 1;
                            }
                            else if (Request.QueryString["Tipo"] == "I" || Request.QueryString["Tipo"] == "E")
                            {
                                tipo = Convert.ToChar(Request.QueryString["Tipo"]);
                                MultiView1.ActiveViewIndex = 0;
                                gestion.Text = gest.ToString();
                                if (Session["ProgramaPresupuesto"] != null)
                                {
                                    Model.Programa p = (Model.Programa)Session["ProgramaPresupuesto"];
                                    codigo = p.CodigoPrograma;
                                    Panel8.Visible = true;
                                    Nombre.Text = p.Nombre;
                                }
                                else
                                    codigo = -1;

                                if (tipo == 'E')
                                {
                                    Panel2.GroupingText = "Egresos";
                                    Panel1.GroupingText = "Agregar Egreso";
                                    DropDownList1.DataSourceID = "";
                                    DropDownList1.DataSource = DataSourceDropDownListEgreso;
                                    DropDownList1.DataBind();
                                }
                                Bind();
                            }
                            else
                            {
                                LabelError.Text = "Tipo no valido";
                                MultiView1.ActiveViewIndex = 1;

                            }


                        }
                    }
                    catch (FormatException)
                    {
                        MultiView1.ActiveViewIndex = 1;
                        LabelError.Text = "No es una gestion valida";
                    }
                    catch (Exception)
                    {
                        MultiView1.ActiveViewIndex = 1;
                        LabelError.Text = "La gestion seleccionada no existe";
                    }
                }


            }
            else
            {
                if (ViewState["tipo"] != null)
                {
                    if (tipo == 'E')
                    {
                        DropDownList1.DataSourceID = "";
                        DropDownList1.DataSource = DataSourceDropDownListEgreso;

                    }
                }
            }
            
            
        }


        protected void Button5_Click(object sender, EventArgs e)
        {
            try
            {
                invicibleIE();
                Processes.Presupuesto.GestorPresupuesto.RegistrarEvento(TextBox10.Text, tipo, TextBox9.Text);
                DropDownList1.DataBind();
                Label15.Text = "se registro el evento " + TextBox9.Text;
                TextBox9.Text = "";
                TextBox10.Text = "";
                Label15.Visible = true;
                Label8.Visible = false;
                
            }
            catch (Exception ex)
            {
                Label8.Text = ex.Message;
                Label15.Visible = false;
                Label8.Visible = true;
            }
         }

        
       
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                invicibleEvento();
                invicibleIE();
                if (e.CommandName == "Eliminar")
                {
                    Label eliminar = (Label)e.Item.FindControl("Label6");
                    Processes.Presupuesto.GestorPresupuesto.EliminarIE(Convert.ToInt64(eliminar.Text), Convert.ToInt32(gest));
                    Bind();
                }
                else
                {
                    Label descripcion = (Label)e.Item.FindControl("Label1");
                    Label valor = (Label)e.Item.FindControl("Label2");
                    Label cantidad = (Label)e.Item.FindControl("Label3");
                    Label total = (Label)e.Item.FindControl("Label4");
                    Label nombre = (Label)e.Item.FindControl("Label7");
                    Label codigo = (Label)e.Item.FindControl("Label6");
                    NombreEditar.Text = nombre.Text;
                    DescripcionEditar.Text = descripcion.Text;
                    TextBox15.Text = valor.Text;
                    TextBox16.Text = cantidad.Text;
                    codigoEvento = codigo.Text;
                    Panel6.Visible = true;
                    Panel2.Visible = false;
                    Panel1.Visible = false;
                }
            }
            catch (Exception)
            { 
            
            }
         //   Label3.Text = l.Text;
        }
        public void invicibleEvento()
        {
            Label8.Visible = false;
            Label15.Visible = false;
        }
        public void invicibleIE()
        {
            Label5.Visible = false;
            Label16.Visible = false;
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            try
            {
                invicibleEvento();
                Processes.Presupuesto.GestorPresupuesto.UsarPresupuesto(gest.ToString(), DropDownList1.SelectedValue, TextBox8.Text, TextBox7.Text,codigo);
                
                Label5.Visible = false;
               
                Label16.Visible = true;
                TextBox7.Text = "";
                TextBox8.Text = "";
            }
            catch (Exception ex)
            {
                Label5.Text = ex.Message;
                Label5.Visible = true;
                Label16.Visible = false;
            }
            finally
            {
                Bind();
            }
        }

        protected void UpdatePanel1_PreRender(object sender, EventArgs e)
        {
           
        }

        protected void UpdatePanel1_Unload(object sender, EventArgs e)
        {
        //    Session["Gestion"] = gest;
        }

        protected void UpdatePanel1_Disposed(object sender, EventArgs e)
        {

        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            Response.Redirect("CrearPresupuesto.aspx");
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            try
            {
                
                Processes.Presupuesto.GestorPresupuesto.EditarPresupuestoUsado(gest,codigoEvento,Convert.ToInt32(TextBox16.Text),Convert.ToDouble(TextBox15.Text));
                ErrorEditar.Visible = false;
                Bind();
                Panel1.Visible = true;
                Panel2.Visible = true;
                Panel6.Visible = false;
            }
            catch (FormatException)
            {
                ErrorEditar.Visible = true;
                ErrorEditar.Text = "Indroduzca valores enteros en cantidad y enteros o decimales en valor";
            }
            catch (Exception ex)
            {
                ErrorEditar.Visible = true;
                ErrorEditar.Text = ex.Message;
            }
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            try
            {
                Panel1.Visible = true;
                Panel2.Visible = true;
                Panel6.Visible = false;
            }
            catch (FormatException)
            {
            }
            catch (Exception ex)
            {

            }
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            if(tipo=='E')
                Response.Redirect("FormularioPresupuesto.aspx?Gestion=" + gest + "&Tipo=I");
            
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            if (tipo == 'I')
                Response.Redirect("FormularioPresupuesto.aspx?Gestion=" + gest + "&Tipo=E");
            
        }

        protected void TextBox8_TextChanged(object sender, EventArgs e)
        {
            
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            
            Response.Redirect("/Programa/BuscarPrograma.aspx?Index=3&Gestion="+gest+"&Tipo="+tipo);
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            codigo = -1;
            Panel8.Visible = false;
        }



       

        





       
    }
}